Crate spidev [] [src]

Spidev

The spidev crate provides access to Linux spidev devices from rust. The wrapping of the interface is pretty direct and shouldn't cause any surprises.

Additional information on the interface may be found in the kernel documentation for spidev.

Examples

extern crate spidev;
use std::io;
use std::io::prelude::*;
use spidev::{Spidev, SpidevOptions, SpidevTransfer, SPI_MODE_0};

fn create_spi() -> io::Result<Spidev> {
    let mut spi = try!(Spidev::open("/dev/spidev0.0"));
    let options = SpidevOptions::new()
         .bits_per_word(8)
         .max_speed_hz(20_000)
         .mode(SPI_MODE_0)
         .build();
    try!(spi.configure(&options));
    Ok(spi)
}

/// perform half duplex operations using Read and Write traits
fn half_duplex(spi: &mut Spidev) -> io::Result<()> {
    let mut rx_buf = [0_u8; 10];
    try!(spi.write(&[0x01, 0x02, 0x03]));
    try!(spi.read(&mut rx_buf));
    println!("{:?}", rx_buf);
    Ok(())
}

/// Perform full duplex operations using Ioctl
fn full_duplex(spi: &mut Spidev) -> io::Result<()> {
    // "write" transfers are also reads at the same time with
    // the read having the same length as the write
    let tx_buf = [0x01, 0x02, 0x03];
    let mut rx_buf = [0; 3];
    {
        let mut transfer = SpidevTransfer::read_write(&tx_buf, &mut rx_buf);
        try!(spi.transfer(&mut transfer));
    }
    println!("{:?}", rx_buf);
    Ok(())
}

fn main() {
    let mut spi = create_spi().unwrap();
    println!("{:?}", half_duplex(&mut spi).unwrap());
    println!("{:?}", full_duplex(&mut spi).unwrap());
}

Reexports

pub use spidevioctl::SpidevTransfer;

Modules

spidevioctl

Structs

SpiModeFlags
Spidev

Provide high-level access to Linux Spidev Driver

SpidevOptions

Options that control defaults for communication on a device

Constants

SPI_3WIRE

SI/SO Signals Shared

SPI_CPHA

Clock Phase

SPI_CPOL

Clock Polarity

SPI_CS_HIGH

Chipselect Active High?

SPI_LOOP

Loopback Mode

SPI_LSB_FIRST

Per-word Bits On Wire

SPI_MODE_0
SPI_MODE_1
SPI_MODE_2
SPI_MODE_3
SPI_NO_CS

1 dev/bus, no chipselect

SPI_READY

Slave pulls low to pause

SPI_RX_DUAL

Receive with 2 wires

SPI_RX_QUAD

Receive with 4 wires

SPI_TX_DUAL

Transmit with 2 wires

SPI_TX_QUAD

Transmit with 4 wires